package com.youbug.system.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youbug.system.entity.Role;
import com.youbug.system.service.IRoleService;
import com.youbug.common.vo.R;
import com.youbug.system.vo.RoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author sunwx
 * @since 2022-12-05
 */
@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private IRoleService roleService;

    @PostMapping("/getRoles")
    public R<IPage<Role>> getRole(@RequestBody RoleVo roleVo){

        IPage<Role> rolePage = roleService.getRolePage(roleVo);

        return R.ok(rolePage);
    }

    @PostMapping("/getRoleOnUser")
    public R<IPage<Role>> getRoleOnUser(@RequestBody RoleVo roleVo){

        IPage<Role> rolePage = roleService.getRoleOnUser(roleVo);

        return R.ok(rolePage);
    }

    @GetMapping("/getRole/{id}")
    public R<Role> getRole(@PathVariable String  id){

        Role user = roleService.getById(id);

        return R.ok(user);
    }

    @GetMapping("/addRoleOnUser/{roleId}/{userId}")
    public R<Boolean> addRoleOnUser(@PathVariable Long roleId, @PathVariable Long userId){

       boolean b = roleService.addRoleOnUser(roleId,userId);

        return R.ok(b);
    }

    @GetMapping("/removeRoleOnUser/{roleId}/{userId}")
    public R<Boolean> removeRoleOnUser(@PathVariable Long roleId, @PathVariable Long userId){

        boolean b =  roleService.removeRoleOnUser(roleId,userId);

        return R.ok(b);
    }

    @PostMapping("/add")
    public R<Boolean> add(@RequestBody Role role){

        boolean save = roleService.save(role);

        return  R.ok(save);
    }
    @GetMapping("/delete/{id}")
    public R<Boolean> add(@PathVariable String  id){

        boolean remove = roleService.removeById(id);

        return  R.ok(remove);
    }

    @PostMapping("/update")
    public R<Boolean> update(@RequestBody Role role){

        boolean save = roleService.updateById(role);

        return  R.ok(save);
    }
}

